<?php

namespace App\Http\Controllers\Strator\Traits;

use App\Models\Strator\StratorAuthToken;
use App\Models\Strator\StratorLoginLog;
use Illuminate\Support\Str;

trait StratorTokenTrait
{

    //
    public function generateToken($request,$strator):string
    {
        $token=Str::random(64);
        $count=StratorAuthToken::where('token',$token)->count();
        if($count>0){
            return $this->generateToken($request,$strator);
        }
        $this->processLoginMode($request,$strator,$token);
        return $token;
    }
    //
    public function processLoginMode($request,$strator,$token):void{
        $mode=config('strator.login_mode');
        switch ($mode){
            case 'single_point':
                $this->singlePointLogin($request,$strator,$token);
                break;
            case 'single_client':
                $this->singleClientLogin($request,$strator,$token);
                break;
            case 'client_limit':
                $this->clientLimitLogin($request,$strator,$token);
                break;
            default:
                break;
        }
        StratorAuthToken::create([
            'strator_id'=>$strator->id,
            'client_type'=>$request->client_type??'',
            'token'=>$token,
            'expires_at'=>date('Y-m-d H:i:s',time()+config('strator.token_expires')),
        ]);
    }
    //
    public function singlePointLogin($request,$strator):void{
        StratorAuthToken::where('strator_id',$strator->id)->delete();
    }
    //
    public function singleClientLogin($request,$strator):void{
        StratorAuthToken::where('strator_id',$strator->id)
            ->where('client_type',$request->client_type??'')
            ->delete();
    }
    //
    public function clientLimitLogin($request,$strator):void{
        $clientCount=StratorAuthToken::where('strator_id',$strator->id)
            ->count();
        $clientLimit=config('strator.client_limit');
        if($clientCount>=$clientLimit){
            $tokens=StratorAuthToken::where('strator_id',$strator->id)
                ->orderBy('id','asc')
                ->limit($clientCount-($clientLimit-1))
                ->get();
            foreach ($tokens as $token){
                $token->delete();
            }
        }
    }

    //
    public function createLoginLog($request,$strator,$result=1,$reason=''):void{
        StratorLoginLog::create([
            'strator_id'=>$strator->id??0,
            'ip'=>$request->ip(),
            'referer'=>$request->header('referer')??$request->header('Referer'),
            'user_agent'=>$request->header('user-agent')??$request->header('User-Agent'),
            'client_type'=>$request->client_type??'',
            'login_result'=>$result,
            'failed_reason'=>$result==0?$reason:'',
            'failed_account'=>$result==0?($request->strator_account??($request->strator_mobile??$request->strator_email)):'',
            'failed_password'=>$result==0?($request->strator_password??''):'',
        ]);
    }

}
